package com.jingxi.rest.service;


import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingxi.common.pojo.EasyUIDataGridResult;
import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.mapper.TbItemMapper;
import com.jingxi.model.TbItem;
import com.jingxi.model.TbItemExample;
import com.jingxi.model.TbItemExample.Criteria;
import com.jingxi.rest.pojo.SolrResult;

@Service
public class SearchServiceImpl implements SearchService {

	private static final ObjectMapper MAPPER = new ObjectMapper();

	/*@Autowired
	private HttpSolrServer httpSolrServer;*/
	
	@Autowired
	TbItemMapper tbItemMapper;

	@Override
	public JingXiResult search(String q, Integer page, Integer rows) {
		TbItemExample tbItemExample = new TbItemExample();
		PageHelper.startPage(page, rows);
		/*Criteria criteria = tbItemExample.createCriteria();
		criteria.andTitleLike("%" + q + "%");*/
		List list = tbItemMapper.selectByExample(tbItemExample);
		PageInfo pageInfo = new PageInfo(list);
		EasyUIDataGridResult easyUIDataGridResult = new EasyUIDataGridResult();
		easyUIDataGridResult.setTotal(pageInfo.getTotal());
		easyUIDataGridResult.setRows(list);
		return JingXiResult.ok(easyUIDataGridResult);
	}
	
	/**
	 * 在索引库中查询商品数据
	 */
	public SolrResult search(String q, Integer page) {
		/*SolrQuery solrQuery = new SolrQuery(); // 构造搜索条件
		solrQuery.setQuery("text:" + q + " AND status:1"); // 搜索关键词
		// 设置分页 start=0就是从0开始，，rows=5当前返回5条记录，第二页就是变化start这个值为5就可以了。
		solrQuery.setStart((Math.max(page, 1) - 1) * 16);
		solrQuery.setRows(16);

		// 是否需要高亮
		boolean isHighlighting = !StringUtils.equals("*", q) && StringUtils.isNotEmpty(q);

		if (isHighlighting) {
			// 设置高亮
			solrQuery.setHighlight(true); // 开启高亮组件
			solrQuery.addHighlightField("title");// 高亮字段
			solrQuery.setHighlightSimplePre("<em>");// 标记，高亮关键字前缀
			solrQuery.setHighlightSimplePost("</em>");// 后缀
		}

		try {
			// 执行查询
			QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
			List<TbItem> items = queryResponse.getBeans(TbItem.class);
			if (isHighlighting) {
				// 将高亮的标题数据写回到数据对象中
				Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
				for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
					for (TbItem item : items) {
						if (!highlighting.getKey().equals(item.getId().toString())) {
							continue;
						}
						item.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
						break;
					}
				}
			}
			// 获取查询到的数据总条数
			long total = queryResponse.getResults().getNumFound();
			// 返回封装好的对象
			return new SolrResult(total, items);
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
		return null;
	}

}
